﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

#endregion

namespace Thinxoft.HttpLib
{
    
    public class HttpApplicationCollection : MarshalByRefObject
    {
        Dictionary<string, HttpApplication> apps;
        Settings settings;

        public HttpApplicationCollection(Settings settings)
        {
            this.settings = settings;
            apps = new Dictionary<string, HttpApplication>();
            foreach (XmlNode app in settings.Configuration["applications"])
            {
                
                CreateApp(app["name"].InnerText, app);
            }
            
        }

        void Refresh()
        {
            apps = new Dictionary<string, HttpApplication>();
            foreach (XmlNode app in settings.Configuration["applications"])
            {

                CreateApp(app["name"].InnerText, app);
            }
        }

        bool CreateApp(string name, XmlNode app)
        {
            if (!apps.ContainsKey(name))
            {
                apps.Add(name, new HttpApplication(app, settings));
                return true;

            }
            return false;
        }

        public bool CreateNewApplication(string name, string root,params string[] defaultdocs)
        {
            try
            {
                XmlDocument xdoc = settings.Configuration.OwnerDocument;
                XmlNode application = (XmlNode)xdoc.CreateElement("application");

                //create name
                XmlNode appname = (XmlNode)xdoc.CreateElement("name");
                appname.InnerText = name;

                //create root
                XmlNode approot = (XmlNode)xdoc.CreateElement("root");
                approot.InnerText = root;

                //create def docs
                XmlNode appdefdocs = (XmlNode)xdoc.CreateElement("defaultdocs");
                foreach (string doc in appdefdocs)
                {
                    XmlNode defdoc = (XmlNode)xdoc.CreateElement("doc");
                    defdoc.InnerText = doc;
                    appdefdocs.AppendChild(defdoc);
                }

                application.AppendChild(appname);
                application.AppendChild(approot);
                application.AppendChild(appdefdocs);

                xdoc.DocumentElement["applications"].AppendChild(application);

                Refresh();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }

        public HttpApplication FindHostApplication(string path)
        {

            HttpApplication temp = null;
            foreach (HttpApplication app in this)
            {
                if (app.IsHostApp(path))
                    temp = app;
            }
            return temp;
        }

        public HttpApplication this[string appName]
        {
            get
            {
                return this.apps[appName];
            }
        }


        public void DeleteApp(string name)
        {
            apps.Remove(name);
        }
        public IEnumerator<HttpApplication> GetEnumerator()
        {
            return this.apps.Values.GetEnumerator();
        }


    }
}
